/* Copyright (c) 2008 LepomisLab.com, Andrey Turkin
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "NSProgressIndicator.h"
#include "NSKeyedUnarchiver.h"
#include "NSWindow.h"
#include "NSCell.h"

#define TIMER_INTERVAL 120
class GaugeTimer : public wxTimer
{
public:
	GaugeTimer::GaugeTimer(NSProgressIndicator *owner) : wxTimer(), _owner(owner)
	{
		Stop();
	}
	void Start(void)
	{
		wxTimer::Start(TIMER_INTERVAL, wxTIMER_CONTINUOUS);
	}

private:
	void GaugeTimer::Notify(void)
	{
		_owner->_Pulse();
	}

	NSProgressIndicator* _owner;
};

IMPLEMENT_DYNAMIC_CLASS(NSProgressIndicator, NSView)

NSProgressIndicator::NSProgressIndicator() : NSView(), _gauge(NULL)
{
	_timer = new GaugeTimer(this);
	exposeBinding(wxT("animate"));
	exposeBinding(wxT("isIndeterminate"));
	exposeBinding(wxT("value"));
	exposeBinding(wxT("maxValue"));
	exposeBinding(wxT("minValue"));
}
NSProgressIndicator::~NSProgressIndicator()
{
	delete _timer;
}

void NSProgressIndicator::initWithUnarchiver(NSKeyedUnarchiver *archiver)
{
	NSView::initWithUnarchiver(archiver);

	if (archiver->contains(wxT("NSMaxValue")))
		_maxVal = archiver->decodeFloatForKey(wxT("NSMaxValue"));

	if (archiver->contains(wxT("NSMinValue")))
		_minVal = archiver->decodeFloatForKey(wxT("NSMinValue"));
	else
		_minVal = 0.0;

	if (archiver->contains(wxT("NSSpiFlags")))
		archiver->decodeIntForKey(wxT("NSSpiFlags"));
}

#define GAUGE_LENGTH 256
wxWindow* NSProgressIndicator::_allocWidget(NSView* parent)
{
	wxASSERT(!_gauge);
	wxWindow* wnd2 = parent->widget();
	wxASSERT(wnd2);
	_gauge = new wxGauge(wnd2, wxID_ANY, GAUGE_LENGTH);
	_initWidget(_gauge);
	return _gauge;
}

void NSProgressIndicator::_releaseWidget(void)
{
	if (!_gauge)
		return;
	_gauge->Destroy();
	_gauge = NULL;
}


void NSProgressIndicator::setSize(const wxRect& newSize)
{
	NSView::setSize(newSize);
	if (_gauge)
		_initWidget(_gauge);
}

void NSProgressIndicator::_updateWidget(void)
{
	if (!_gauge || _indeterminate)
		return;
	double val = _curVal;
	if (val<_minVal) val = _minVal;
	else if (val>_maxVal) val = _maxVal;
	int ival = (val-_minVal)*GAUGE_LENGTH/(_maxVal-_minVal);
	_gauge->SetValue(ival);
}

void NSProgressIndicator::_Pulse(void)
{
	if (!_gauge || !_indeterminate)
		return;
	_gauge->Pulse();
}

void NSProgressIndicator::_setValue(const wxString& key, wxObject *value)
{
	if (key == wxT("value"))
	{
		if (value)
		{
			wxVariant *v = wxDynamicCast(value, wxVariant);
			wxASSERT(v);
			_curVal = v->GetDouble();
			_updateWidget();
		}
	} else if (key == wxT("maxValue"))
	{
		if (value)
		{
			wxVariant *v = wxDynamicCast(value, wxVariant);
			wxASSERT(v);
			_maxVal = v->GetDouble();
			_updateWidget();
		}
	} else if (key == wxT("minValue"))
	{
		if (value)
		{
			wxVariant *v = wxDynamicCast(value, wxVariant);
			wxASSERT(v);
			_minVal = v->GetDouble();
			_updateWidget();
		}
	} else if (key == wxT("animate"))
	{
		if (value)
		{
			wxVariant *v = wxDynamicCast(value, wxVariant);
			wxASSERT(v);
			bool _new_animate = v->GetBool();
			if (_animate != _new_animate)
			{
				_animate = _new_animate;
				if (_indeterminate)
				{
					if (_animate)
						_timer->Start();
					else
						_timer->Stop();
				}
			}
		}
	} else if (key == wxT("isIndeterminate"))
	{
		if (value)
		{
			wxVariant *v = wxDynamicCast(value, wxVariant);
			wxASSERT(v);
			bool _new_indeterminate = v->GetBool();
			if (_indeterminate != _new_indeterminate)
			{
				_indeterminate = _new_indeterminate;
				if (_indeterminate)
				{
					_Pulse(); /* not very good but wxWidgets' gauge cannot just switch to indeterminate without moving */
					if (_animate)
						_timer->Start();
				} else
				{
					if (_timer->IsRunning())
						_timer->Stop();
					_updateWidget();
				}
			}
		}
	} else
		NSView::_setValue(key, value);
}

wxObject* NSProgressIndicator::_getValue(const wxString& key)
{
	if (key == wxT("value"))
		return new wxVariant(_curVal);
	else if (key == wxT("maxValue"))
		return new wxVariant(_maxVal);
	else if (key == wxT("minValue"))
		return new wxVariant(_minVal);
	else if (key == wxT("animate"))
		return new wxVariant(_animate);
	else if (key == wxT("isIndeterminate"))
		return new wxVariant(_indeterminate);
	else
		return NSView::_getValue(key);
}
